package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 查询用户资料
     * @param token
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token, Long userId){
        if (userId == null){

            userId = UserHolder.getUserId();
        }
        // 根据id查询
        UserInfoVo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     * @param userInfo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        // 设置用户id
        userInfo.setId(UserHolder.getUserId());
        // 调用service
        userInfoService.updateById(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     *  互相喜欢,统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map<String,Integer> map = userInfoService.findCounts();
        return ResponseEntity.ok(map);
    }
}
